package com.DFB.controller;


import com.DFB.common.model.Result;
import com.DFB.common.model.ResultCode;
import com.DFB.pojo.Group;
import com.DFB.service.GroupService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 店铺员工分组列表 前端控制器
 * </p>
 *
 * @author DFB
 * @since 2023-09-11
 */
@RestController
@Slf4j
@RequestMapping("/group")
public class GroupController {

    @Autowired
    GroupService groupService;

    /**
     * 获取店铺的员工分组
     *
     * @param storeId
     * @return
     */
    @PostMapping("/staffGroup")
    public Result<?> getStaffGroup(@RequestBody String storeId) {
        LambdaQueryWrapper<Group> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Group::getStoreId, storeId);
        return Result.success(groupService.list(wrapper));
    }

    /**
     * 新增店铺的员工分组
     *
     * @param group
     * @return
     */
    @PostMapping("/addGroup")
    public Result<?> addGroup(@RequestBody Group group) {
        try {
            groupService.save(group);
            return Result.success();
        } catch (Exception e) {
            log.info("Error occurred while adding group: " + e.getMessage());
            return Result.error(ResultCode.DATA_ALREADY_EXISTED);
        }
    }

    /**
     * 删除分组
     *
     * @param groupId
     * @return
     */
    @PostMapping("/delGroup")
    public Result<?> delGroup(@RequestBody String groupId) {
        try {
            groupService.removeById(groupId);
            return Result.success("del success");
        } catch (Exception e) {
            log.info("Error occurred while delete group: "+e.getMessage());
            return Result.error(ResultCode.RESULT_DATA_NONE);
        }
    }

    /**
     * 修改分组
     *
     * @param group
     * @return
     */
    @PostMapping("/updateGroup")
    public Result<?> updateGroup(@RequestBody Group group) {
        try {
            groupService.updateById(group);
            return Result.success();
        } catch (Exception e) {
            log.info("Error occurred while updating group: "+e.getMessage());
            return Result.error(ResultCode.DATA_IS_WRONG);
        }
    }
}

